package com.ezjie.ielts.module_read;

import java.util.ArrayList;
import java.util.Collections;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.ezjie.ielts.R;
import com.ezjie.ielts.constant.Constants;
import com.ezjie.ielts.core.base.BaseFragmentActivity;
import com.ezjie.ielts.core.http.HttpRequestAbstractCallBack;
import com.ezjie.ielts.core.spfs.SharedPrefHelper;
import com.ezjie.ielts.db.UserInfoDB;
import com.ezjie.ielts.model.FullTextInfo;
import com.ezjie.ielts.model.GroupInfo;
import com.ezjie.ielts.model.ReadClassifyQuestionInfo;
import com.ezjie.ielts.task.ErrorQuestionTask;
import com.ezjie.ielts.util.AppUtil;
import com.ezjie.ielts.util.LogUtils;
import com.ezjie.ielts.util.OnDialogBtnClickImpl;
import com.ezjie.ielts.util.UmengPages;
import com.ezjie.ielts.widget.AppWarnDialog;
import com.ezjie.ielts.widget.TitleTextView;
import com.ezjie.login.LoginActivity;
import com.ezjie.login.model.UserDetail;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.umeng.analytics.MobclickAgent;

@ContentView(R.layout.activity_read_main)
public class ReadMainActivity extends BaseFragmentActivity {

	@ViewInject(R.id.iv_topbar_back)
	private ImageView iv_topbar_back;
	@ViewInject(R.id.tv_topbar_title)
	private TitleTextView tv_topbar_title;
	@ViewInject(R.id.btn_exam_layout)
	private Button btn_exam_layout;
	@ViewInject(R.id.btn_classify_layout)
	private Button btn_classify_layout;
	@ViewInject(R.id.btn_error_layout)
	private Button btn_error_layout;
	@ViewInject(R.id.tv_error_question)
	private TextView tv_error_question;

	private Context mContext;
	private UserInfoDB userInfoDB;
	private UserDetail userDetail;
	private ErrorQuestionTask mErrorQuestionTask;
	private ArrayList<GroupInfo> data = new ArrayList<GroupInfo>();
	private ArrayList<FullTextInfo> origindata = new ArrayList<FullTextInfo>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ViewUtils.inject(this);

		mContext = this;
		userInfoDB = new UserInfoDB(this);
		mErrorQuestionTask = new ErrorQuestionTask(this);

		init();
	}

	private void init() {
		tv_topbar_title.setText(R.string.read_practice);
		iv_topbar_back.setOnClickListener(this);
		btn_exam_layout.setOnClickListener(this);
		btn_classify_layout.setOnClickListener(this);
		btn_error_layout.setOnClickListener(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onPageStart(UmengPages.PAGE_READ);
		if (userDetail == null) {
			userDetail = userInfoDB.queryLoginUser();
		}
		String dailynum = SharedPrefHelper.getInstance().getDailyNum(userDetail.uid);
		String finishnum = SharedPrefHelper.getInstance().getFinishNum(userDetail.uid);
		tv_error_question.setText(String.format(getResources().getString(R.string.done_practice), dailynum, finishnum));
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPageEnd(UmengPages.PAGE_READ);
	}

	@Override
	public void onClickEvent(View view) {
		switch (view.getId()) {
		case R.id.iv_topbar_back:
			finish();
			break;
		case R.id.btn_exam_layout:
			startActivity(new Intent(mContext, ReadExamActivity.class));
			break;
		case R.id.btn_classify_layout:
			startActivity(new Intent(mContext, ReadClassifyActivity.class));
			break;
		case R.id.btn_error_layout:
			// 判断用户是否登录
			if (userInfoDB != null) {
				userDetail = userInfoDB.queryLoginUser();
				if (userDetail != null && userDetail.islogin == 1) {
					// 已登录
				} else {
					startActivity(new Intent(this, LoginActivity.class));
					return;
				}
			}
			// startActivity(new Intent(mContext, ReadErrorActivity.class));
			getErrorQuestionList();
			break;
		default:
			break;
		}
	}

	private void getErrorQuestionList() {
		mErrorQuestionTask.getErrorQuestions(Constants.READ_ERROR_TYPE_VALUE, new HttpRequestAbstractCallBack(mContext,false) {
			@Override
			public void onStartCallBack() {
				super.onStartCallBack();
				AppUtil.showProgressDialog(mContext);
			}

			@Override
			public void onSuccessCallBack(String result) {
				super.onSuccessCallBack(result);
				LogUtils.d(result);
				AppUtil.dismissProgressDialog();
				try {
					ReadClassifyQuestionInfo info = JSON.parseObject(result, ReadClassifyQuestionInfo.class);
					if (null != info.data && null != info.data.questions) {
						data = info.data.questions;
						if ("1".equals(SharedPrefHelper.getInstance().isRand(userDetail.uid))) {
							Collections.shuffle(data);
						}
						origindata = info.data.full_contents;
						if (data.size() > 0) {
							Bundle bundle = new Bundle();
							Intent intent = new Intent(mContext, ReadAllPracticeActivity.class);
							intent.putExtra(Constants.READ_ERROR_TYPE, Constants.READ_ERROR_TYPE_VALUE);
							intent.putExtra(Constants.READ_FROM, Constants.READ_FROM_VALUE3);
							bundle.putSerializable(Constants.READ_SETDATA_PRACTICE, data);
							bundle.putSerializable(Constants.READ_SETDATA_ORIGIN, origindata);
							intent.putExtra(Constants.READ_ERROR_BUNDLE, bundle);
							startActivity(intent);
						} else {
							AppWarnDialog appWarnDialog = new AppWarnDialog(mContext, R.style.customDialog);
							appWarnDialog.show();
							appWarnDialog.setOneButton(true);
							appWarnDialog.setCancelable(false);
							appWarnDialog.setMessage(R.string.read_error_dialog_title);
							appWarnDialog.setKnowButton(R.string.read_error_dialog_ok);
							appWarnDialog.setListener(new OnDialogBtnClickImpl() {
								@Override
								public void onKnowClick() {
								};
							});
						}
					}
				} catch (Exception e) {
					LogUtils.exception(e);
				}
			}

			@Override
			public void onFailureCallBack(HttpException error, String msg) {
				super.onFailureCallBack(error, msg);
				LogUtils.e(msg);
				AppUtil.dismissProgressDialog();
				AppUtil.showToast(ReadMainActivity.this, R.string.load_net_data_failure);
			}
		});
	}
}
